package com.teziyou.commodity.web;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import jodd.bean.BeanUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.mytravel.platform.bean.DictionaryBean;
import com.teziyou.commodity.bean.Page;
import com.teziyou.commodity.constant.Global;
import com.teziyou.commodity.enumtype.CategoryEnum;
import com.teziyou.commodity.logic.CategoryLogic;
import com.teziyou.commodity.logic.ShelfLogic;
import com.teziyou.commodity.logic.TicketLogic;
import com.teziyou.commodity.service.DictService;
import com.teziyou.commodity.util.DateUtil;
import com.teziyou.commodity.util.Lang;
import com.teziyou.commodity.util.Maps;
import com.teziyou.commodity.util.PageUtil;

@SuppressWarnings({"rawtypes", "unchecked"})
@RequestMapping("/ticket")
@Controller
public class TicketController extends BaseController {
	
	@Autowired
	private TicketLogic ticketLogic;
	@Autowired
	private ShelfLogic shelfLogic;
	@Autowired
	private DictService dictService;
	@Autowired
	private CategoryLogic categoryLogic;
	
	@RequestMapping("/index")
	public String index(HttpServletRequest req) {
		Page ticketPage = ticketLogic.listTickets(super.getParaMap(req));
		req.setAttribute("ticketPage", ticketPage);
		return "ticket/index";
	}
	
	@RequestMapping("/listdata")
	public String listData(HttpServletRequest req) {
		Page ticketPage = ticketLogic.listTickets(super.getParaMap(req));
		req.setAttribute("ticketPage", ticketPage);
		return "ticket/ticket_table";
	}
	
	@RequestMapping("/relationTickets")
	public String relationTickets(HttpServletRequest req) {
		Page ticketPage = ticketLogic.listTickets(super.getParaMap(req));
		req.setAttribute("ticketPage", ticketPage);
		return "relation/ticket_table";
	}
	
	@ResponseBody
	@RequestMapping("/saveRelations")
	public String saveRelations(HttpServletRequest req) {
		try {
			ticketLogic.saveRelations(super.getParaMap(req));
			return success("保存成功！！");
		} catch (Exception e) {
			e.printStackTrace();
			return fail("保存失败！！");
		}
	}
	
	@RequestMapping("/addview")
	public String addView(ModelMap model) {
		model.addAttribute("types", dictService.getDictionaryList(Global.DictType.ticket_type));
		return "/ticket/add";
	}
	
	@ResponseBody
	@RequestMapping("/add")
	public String addTicket(HttpServletRequest req) {
		try {
			ticketLogic.addTicket(super.getParaMap(req));
			return success("保存成功！！");
		} catch (Exception e) {
			e.printStackTrace();
			return fail("保存失败！！");
		}
	}
	
	@RequestMapping("/loaddatas")
	public String loadDatas(HttpServletRequest req) {
		List list = ticketLogic.loadTickets(req.getParameter("ids"));
		req.setAttribute("tickets", list);
		return "relation/ticket_index_table";
	}
	
	@ResponseBody
	@RequestMapping("/delTickets")
	public String delete(HttpServletRequest req) {
		try {
			String[] ticketIds = req.getParameterValues("ticketId");
			ticketLogic.deleteTickets(ticketIds);
			return success("删除成功！！");
		} catch (Exception e) {
			e.printStackTrace();
			return fail("删除失败！！");
		}
	}
	
	@RequestMapping("/dialog")
	public String dialog(HttpServletRequest req) {
		Page ticketPage = ticketLogic.listTickets(super.getParaMap(req));
		req.setAttribute("ticketPage", ticketPage);
		return "ticket/dialog";
	}
	
	@RequestMapping("/dialogPage")
	public String dialogPage(HttpServletRequest req) {
		Page ticketPage = ticketLogic.listTickets(super.getParaMap(req));
		req.setAttribute("ticketPage", ticketPage);
		return "ticket/dialog_table";
	}
	
	@RequestMapping("/ticketRelations")
	public String ticketRelations(String id, ModelMap model) {
		model.addAttribute("tickets", ticketLogic.findRelateTicketsByTicketId(Long.parseLong(id)));
		return "relation/ticket_index_table";
	}
	
	@ResponseBody
	@RequestMapping("/bindRelateTickets")
	public String bindRelateTickets(long ticketId, String ticketIdStr) {
		try {
			List tickets = ticketLogic.findRelateTicketsByTicketId(ticketId);
			if(ticketIdStr.contains(ticketId+""))
				return fail("请勿关联自身！！");
			if(!CollectionUtils.isEmpty(tickets)) {
				Iterator iter = tickets.iterator();
				boolean bool = false;
				while(iter.hasNext()) {
					Map ticket = (Map) iter.next();
					String id = Lang.toString(ticket.get("ticket_info_id"));
					if(ticketIdStr.indexOf(id)!=-1) {
						bool = true;
						break;
					}
				}
				if(bool)
					return fail("请勿重复关联票！！");
			}
			ticketLogic.bindRelateTickets(ticketId, ticketIdStr);
			return success("关联成功！！");
		} catch (Exception e) {
			e.printStackTrace();
			return fail("关联失败！！");
		}
	}
	
	@ResponseBody
	@RequestMapping("/deleteTicketTicketRelation")
	public String deleteTicketTicketRelation(HttpServletRequest req) {
		try {
			ticketLogic.deleteTicketTicketRelation(super.getParaMap(req));
			return success("删除成功！！");
		} catch (Exception e) {
			e.printStackTrace();
			return fail("删除失败！！");
		}
	}
	
	@RequestMapping("/editView")
	public String editView(long id, ModelMap model) {
		model.addAttribute("types", dictService.getDictionaryList(Global.DictType.ticket_type));
		model.addAttribute("ticket", ticketLogic.loadTicketById(id));
		return "ticket/add";
	}
	
	@ResponseBody
	@RequestMapping("/update")
	public String update(HttpServletRequest req) {
		try {
			ticketLogic.updateTicket(super.getParaMap(req));
			return success("保存成功！！");
		} catch (Exception e) {
			e.printStackTrace();
			return fail("保存失败！！");
		}
	}
	
	/************************************上下架**************************************************/
	
	@RequestMapping("/shelf/index")
	public String shelfIndex(HttpServletRequest req) {
		Page onsaleTicketPage = ticketLogic.findOnSaleTickets(super.getParaMap(req));
		req.setAttribute("onsaleTicketPage", onsaleTicketPage);
		
		return "ticket/shelf/index";
	}
	
	@RequestMapping("/shelf/loadticket")
	public String loadTicket(HttpServletRequest req) {
		long ticketId = Long.parseLong(req.getParameter("id"));
		Map ticket = shelfLogic.loadTicket(ticketId);
		req.setAttribute("ticket", ticket);
		return "ticket/shelf/shelf";
	}
	
	@RequestMapping("/shelf/relateTicketsDialog")
	public String relatedTicketsDialog(long id, ModelMap model) {
		Page ticketPage = shelfLogic.getRelateTickets(1, id);
		model.addAttribute("ticketPage", ticketPage);
		return "relation/ticket_dialog";
	}
	
	@RequestMapping("/shelf/relatedTickets")
	public String relatedTickets(HttpServletRequest req) {
		int pageNo = PageUtil.initPageNo(req.getParameter("pageNo"));
		long id = Long.parseLong(req.getParameter("id"));
		Page ticketPage = shelfLogic.getRelateTickets(pageNo, id);
		req.setAttribute("ticketPage", ticketPage);
		return "relation/ticket_dialog_table";
	}
	
	@RequestMapping("/shelf/relatedlines")
	public String relatedLines() {
		return null;
	}
	
	@RequestMapping("/shelf/list")
	public String list(HttpServletRequest req) {
		Page ticketPage = ticketLogic.listTickets(super.getParaMap(req));
		req.setAttribute("ticketPage", ticketPage);
		return "ticket/shelf/list";
	}
	
	@RequestMapping("/shelf/addview")
	public String jumpAdd(ModelMap model) {
		model.addAttribute("payTypes", dictService.getDictionaryList(Global.DictType.pay_type));
		return "ticket/shelf/add";
	}
	
	@RequestMapping("/shelf/ticket")
	public String jumpTicket(HttpServletRequest req) {
		Page ticketPage = ticketLogic.listTickets(super.getParaMap(req));
		req.setAttribute("ticketPage", ticketPage);
		return "ticket/shelf/ticket";
	}
	
	@RequestMapping("/shelf/loadRelatedTickets")
	public String loadRelatedTickets(HttpServletRequest req) {
		return "ticket/shelf/ticket";
	}
	
	@RequestMapping("/shelf/ticketDetail")
	public String detail(String id, ModelMap model) {
		Map ticket = ticketLogic.findTicketById(Long.parseLong(id));
		model.addAttribute("ticket", ticket);
		return "ticket/shelf/ticket_view";
	}
	
	@ResponseBody
	@RequestMapping("shelf/save")
	public String saveShelf(HttpServletRequest req) {
		try {
			shelfLogic.saveShelfTicket(super.getParaMap(req));
			return success("保存成功！！");
		} catch (Exception e) {
			e.printStackTrace();
			return fail("保存失败！！");
		}
	}
	
	@ResponseBody
	@RequestMapping("shelf/changeShelfStatus")
	public String changeShelfStatus(String id, int isok) {
		String msg = isok==1?"下架":"上架";
		try {
			shelfLogic.updateShelfStatus(id, isok);
			return success(msg+"成功！！");
		} catch (Exception e) {
			e.printStackTrace();
			return fail(msg+"失败！！");
		}
	}
	
	@RequestMapping("/shelf/page")
	public String detail(HttpServletRequest req) {
		System.out.println(super.getParaMap(req));
		Page onsaleTicketPage = ticketLogic.findOnSaleTickets(super.getParaMap(req));
		req.setAttribute("onsaleTicketPage", onsaleTicketPage);
		return "ticket/shelf/table";
	}
	
	/*************************************分类*********************************************/
	
	@RequestMapping("/category/index")
	public String categoryIndex(Map map) {
		map.put("trees", JSON.toJSONString(categoryLogic.getCategoryTree(CategoryEnum.TICKET.getCode())));
		return "ticket/category/index";
	}
	
	@RequestMapping("/category/load")
	public String loadCategory(int id, Map map) {
		DictionaryBean dict = dictService.getDictionaryById(id);
		map.put("dict", dict);
		return "ticket/category/form";
	}
	
	@ResponseBody
	@RequestMapping("/category/save")
	public String saveCategory(HttpServletRequest req) {
		Map param = super.getParaMap(req);
		param.put("orderNO", Lang.toInt(param.get("orderNO")));
		DictionaryBean dict = new DictionaryBean();
		BeanUtil.populateBean(dict, param);
		try {
			categoryLogic.saveCategory(dict);
			System.err.println(dict);
			return success("保存成功！！", categoryLogic.getCategoryTree(CategoryEnum.TICKET.getCode()));
		} catch (Exception e) {
			e.printStackTrace();
			return fail("保存失败！！");
		}
	}
	
	@ResponseBody
	@RequestMapping("/category/del")
	public String deleteCategory(int id) {
		try {
			dictService.deleteDictionaryTrift(id);
			return success("删除成功！！", categoryLogic.getCategoryTree(CategoryEnum.TICKET.getCode()));
		} catch (Exception e) {
			e.printStackTrace();
			return fail("删除失败！！");
		}
		
	}
	
	/*********************************票务广告*************************************/
	@RequestMapping("/ad/index")
	public String adIndex(HttpServletRequest req) {
		Page adPage = ticketLogic.findAds(super.getParaMap(req));
		req.setAttribute("adPage", adPage);
		return "ticket/ad/index";
	}
	
	@RequestMapping("/ad/addView")
	public String addView() {
		return "ticket/ad/add";
	}
	
	@RequestMapping("/ad/loadScene")
	public String loadScene() {
		return "ticket/ad/scene";
	}
	
	@ResponseBody
	@RequestMapping("/ad/save")
	public String saveAd(HttpServletRequest req) {
		try {
			Map map = super.getParaMap(req);
			System.err.println(map);
			ticketLogic.saveAd(map);
			return success("保存成功！！");
		} catch (Exception e) {
			e.printStackTrace();
			return fail("保存失败！！");
		}
	}
	
	@ResponseBody
	@RequestMapping("/ad/changeStatus")
	public String changeStatus(long id, int isok) {
		String oper = isok==1?"下架":"上架";
		int flag = isok==1?0:1;
		String key = isok==1?"unlauchTime":"launchTime";
		try {
			ticketLogic.updateAd(Maps.newHashMap("ticketAdsId", id, "isok", flag
					, key, DateUtil.formatDate(new Date(), DateUtil.pattern1)));
			return success(oper+"成功！！");
		} catch (Exception e) {
			e.printStackTrace();
			return fail(oper+"失败！！");
		}
	}
	
}
